﻿using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization;
using System.Windows;
using System.Xml;
using System.Xml.Serialization;
using JustinAngel.Neurons.ServerModel;

namespace JustinAngel.Neurons.Framework.Helpers
{
    public static class SerializationHelper
    {
        public static T FromString<T>(string xml)
            where T : class
        {
            try
            {
                return (T)new XmlSerializer(typeof(T)).Deserialize(xml.ToStream());
            }
            catch(Exception ex)
            {
                return null;
            }
        }

        public static string ToXmlString<T>(T instance)
            where T : class
        {
            try
            {
                using (StringWriter sw = new StringWriter())
                {
                   new XmlSerializer(typeof (T)).Serialize(sw, instance);
                    return sw.ToString();
                }

            }
            catch (Exception ex)
            {
                return null;
            }
        }

    }
}
